package com.jjvu.news.controller;

import com.github.pagehelper.PageInfo;
import com.jjvu.news.dao.Info;
import com.jjvu.news.dao.MaxMenu;
import com.jjvu.news.dao.MinMenu;
import com.jjvu.news.service.InfoDetailService;
import com.jjvu.news.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class InfoController {
    @Autowired
    private InfoDetailService infoDetailService;
    @Autowired
    private MenuService menuService;
    @RequestMapping("/findInfoPageAll")
    public String findInfoPageAll(@RequestParam(value = "page",required = true,defaultValue = "1")Integer page,
                                  @RequestParam(value = "size",required = true,defaultValue = "5")Integer size,Model model){
        List<Info> listInfo=infoDetailService.findInfoPageAll(page,size);
        PageInfo<Info> pageInfo=new PageInfo<Info>(listInfo);
        Integer pageIndex=1;
        model.addAttribute("listInfo",listInfo);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("pageIndex", pageIndex);
        return "admin/InfoManager";
    }

    @RequestMapping("/findInfoById")
    public String findInfoById(Integer info_id,Model model){
        Info info=infoDetailService.findInfoById(info_id);
        //info_id查找max_name,min_name
        Info nameGroup=infoDetailService.findNameByInfoId(info_id);
        //全查maxMenu
        List<MaxMenu> maxMenuAll = menuService.findMaxMenuAll();
        model.addAttribute("listMaxMenu",maxMenuAll);
        model.addAttribute("nameGroup",nameGroup);
        model.addAttribute("info",info);
        return "admin/InfoEdit";
    }
//    级联菜单
    //根据maxid查询minMenu
    @RequestMapping(value = "/getMinList/{max_id}",method = RequestMethod.GET)
    @ResponseBody
    public Object getMinList(@PathVariable("max_id")Integer max_id){
        List<MaxMenu> maxMenu=menuService.findMinMenuByMaxName(max_id);
        List<MinMenu> listMinMenu = null;
        System.out.println(maxMenu);
        try {
            listMinMenu=maxMenu.get(0).getMinMenus();
        }catch (Exception e){
            e.getMessage();
        }
        System.out.println(listMinMenu);
        return listMinMenu;
    }
    @RequestMapping("/updateInfo")
    public String updateInfo(Info info){
        if (infoDetailService.updateInfo(info)){
            return "redirect:/findInfoPageAll";
        }
        return "forward:/findInfoById?info_id="+info.getInfo_id();
    }

    @RequestMapping("/toAddInfo")
    public String toAddInfo(Model model){
        List<MaxMenu> listMaxMenu = menuService.findMaxMenuAll();
        model.addAttribute("listMaxMenu",listMaxMenu);
        return "admin/AddInfo";
    }
    @RequestMapping("/addInfo")
    public String addInfo(Info info){
        if (infoDetailService.addInfo(info)){
            return "redirect:/findInfoPageAll";
        }
        return "forward:/toAddInfo";
    }
    @RequestMapping(value = "/deleltInfoByListId/{arrType}",method = RequestMethod.GET)
    @ResponseBody
    public void deleteInfo(@PathVariable("arrType") List<Integer> arrType){
        System.out.println(arrType);
        infoDetailService.deleteInfo(arrType);
    }
}
